package com.qdlc.p2p.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

import javax.net.ssl.HttpsURLConnection;

/**
 * 工具类-Http请求
 * 
 * @author xx
 * @version 1.0
 */
public class HttpUtil {
	/**
	 * 发起http请求，获取响应结果
	 * 
	 * @param pageURL
	 * @return
	 */
	public static String getHttpResponse(String pageURL) {
		String pageContent = "";
		BufferedReader in = null;
		InputStreamReader isr = null;
		InputStream is = null;
		HttpURLConnection huc = null;
		try {
			URL url = new URL(pageURL);
			huc = (HttpURLConnection) url.openConnection();
			is = huc.getInputStream();
			isr = new InputStreamReader(is);
			in = new BufferedReader(isr);
			String line = null;
			while (((line = in.readLine()) != null)) {
				if (line.length() == 0)
					continue;
				pageContent += line;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
				if (isr != null)
					isr.close();
				if (in != null)
					in.close();
				if (huc != null)
					huc.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return pageContent;
	}

    public static String getHttpsResponse(String pageURL) {
        String pageContent = "";
        BufferedReader in = null;
        InputStreamReader isr = null;
        InputStream is = null;
        HttpsURLConnection huc = null;
        try {
            URL url = new URL(pageURL);
            huc = (HttpsURLConnection) url.openConnection();
            is = huc.getInputStream();
            isr = new InputStreamReader(is);
            in = new BufferedReader(isr);
            String line = null;
            while (((line = in.readLine()) != null)) {
                if (line.length() == 0)
                    continue;
                pageContent += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
                if (isr != null)
                    isr.close();
                if (in != null)
                    in.close();
                if (huc != null)
                    huc.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return pageContent;
    }

    public static <T> T getJson(String url, Class<T> clazz) {
		HttpGet httpGet = new HttpGet(url);
		httpGet.addHeader("Accept",
				"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		httpGet.addHeader("Connection", "Keep-Alive");
		httpGet.addHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
		httpGet.addHeader("Cookie", "");

		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			CloseableHttpResponse response = httpclient.execute(httpGet);

			if (response == null) {
				return null;
			}
			String result = EntityUtils.toString(response.getEntity());
			return JSON.parseObject(result.toString().trim(), clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 发送post请求
	 */
	public static String doPost(String url, NameValuePair[] data) {
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(url);
		BufferedReader br = null;
		try {
			method.setRequestBody(data);
			client.executeMethod(method);
			br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
			String temp;
			StringBuilder result = new StringBuilder(1024);
			while((temp=br.readLine()) != null){
				result.append(temp);
			}
			return result.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return "";
	}
}
